//
//  ViewController.m
//  BSProject
//
//  Created by nsm on 2019/6/17.
//  Copyright © 2019 beisheng. All rights reserved.
//

#import "ViewController.h"
#import "RegistController.h"
#import "NSMCitySelectorController.h"
#import "VipAddrSearchHomeController.h"
#import "VipAddrSearchHomeController.h"
#import "VipAddrSearchAssociateController.h"
#import "VipAddrSearchResultController.h"
#import "NSMSearchHomeController.h"
#import "WithdrawController.h"
#import "NSMAssetController.h"

@interface TestCell : NSMTableViewCell
@property (nonatomic, strong) UILabel *titleLabel;
@end

@implementation TestCell
- (void)setObject:(id)obj {
    _titleLabel.text = SafeString(obj[@"title"]);
}

- (void)setupSubviews {
    MyLinearLayout *layout = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    layout.myHorzMargin = 0;
    layout.cacheEstimatedRect = YES;
    layout.padding = UIEdgeInsetsMake(15, 15, 15, 15);
    [self.contentView addSubview:layout];
    self.rootLayout = layout;
    
    UILabel *label = UILabel.new;
    label.myHorzMargin = 0;
    label.wrapContentHeight = YES;
    label.font = CustomFont(14);
    label.textColor = COLOR_0;
    [layout addSubview:label];
    _titleLabel = label;
}

@end


@interface ViewController () <UIPopoverPresentationControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *popItem = [[UIBarButtonItem alloc] initWithTitle:@"弹框" style:UIBarButtonItemStylePlain target:self action:@selector(onClickPopItem:)];
    self.navigationItem.rightBarButtonItem = popItem;
    
    self.tableView.sourceManager.classForCellAtIndexPath = ^Class(NSIndexPath *indexPath) {
        return TestCell.class;
    };
    NSArray *items = @[@{@"title": @"修改密码(含验证码按钮使用规范)", @"sel": @"regist"},
                       @{@"title": @"城市选择", @"sel": @"citySelector"},
                       @{@"title": @"结果验证测试专用", @"sel": @"pushTest"},
                       @{@"title": @"分页滑动", @"sel": @"pageTest"},
                       @{@"title": @"常规搜索1(区分历史记录,vip万达)", @"sel": @"searchNormal1"},
                       @{@"title": @"常规搜索2(区分历史记录,vip万达)", @"sel": @"searchNormal2"},
                       @{@"title": @"常规搜索3(vip万达)", @"sel": @"searchNormal3"},
                       @{@"title": @"非常规搜索(菲度vip地址功能,vip万达)", @"sel": @"searchSpecial"},
                       @{@"title": @"个人简历提交", @"sel": @"testImages:"},
                       @{@"title": @"个人简历展示", @"sel": @"testImages1:"},
                       @{@"title": @"提现", @"sel": @"testWithdraw:"},
                       @{@"title": @"多代理(看log)", @"sel": @"testDelegate:"},
                       @{@"title": @"WebSocket", @"sel": @"testWebSocket:"},
                       @{@"title": @"socket", @"sel": @"testSocket:"},
                       @{@"title": @"textField测试", @"sel": @"testTextField:"},
                       @{@"title": @"进度条测试", @"sel": @"testProgress:"},
                       @{@"title": @"图片选择", @"sel": @"imagePick:"},
                       @{@"title": @"统计电话时长", @"sel": @"getPhoneDuration:"},
                       @{@"title": @"直播短视频滑动", @"sel": @"pageScroll:"}
    ];
    self.tableView.sourceManager.dataArray = items.mutableCopy;
    self.view.backgroundColor = GROUP_TABLECOLOR;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    id obj = tableView.sourceManager.dataArray[indexPath.row];
    SEL sel = NSSelectorFromString(obj[@"sel"]);
    [self performSelectorOnMainThread:sel withObject:obj waitUntilDone:NO];
}

- (void)regist {
    RegistController *vc = RegistController.new;
    vc.title = @"修改密码";
    vc.apiType = ApiTypeModifyLogin;
    // vc.mobileEditEnabled = NO;
    vc.components = @[@(ComponentMobile),
                      @(ComponentSMSCode),
                      @(ComponentPassword)];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)citySelector {
    NSMCitySelectorController *vc = NSMCitySelectorController.new;
    vc.title = @"城市选择";
    vc.moudles = @[@(NSMCSMoudleSelect),
                   @(NSMCSMoudleRecent),
                   @(NSMCSMoudleHot)];
    
    // vc = NSClassFromString(@"NSMChooseCityViewController").new;
    
    NSMNavigationController *nav = [NSMNavigationController controller:vc];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    dispatch_async_on_main_queue(^{
        [self presentViewController:nav animated:YES completion:nil];
    });
}


- (void)pushTest {
    UIViewController *vc = NSClassFromString(@"TestController").new;
    vc.title = @"测试用";
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)pageTest {
    UIViewController *vc = NSClassFromString(@"PageViewController").new;
    vc.title = @"分页";
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)searchNormal1 {
    NSMSearchHomeController *home = NSMSearchHomeController.new;
    home.historyCacheKey = @"searchNormal1";
    
    NSMSearchController *search = NSMSearchController.new;
    search.homeController = home;
    search.associateController.associateURL = @"common/getVipAddressList";
    search.resultController = VipAddrSearchResultController.new;
    NSMNavigationController *nav = [NSMNavigationController controller:search];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)searchNormal2 {
    NSMSearchHomeController *home = NSMSearchHomeController.new;
    home.historyCacheKey = @"searchNormal2";
    // 以后热门搜索接口需独立,不能放公共接口
    home.urlString = @"http://xuehua.test.hbbeisheng.com/api/publics/globalParam";
    
    NSMSearchController *search = NSMSearchController.new;
    search.homeController = home;
    search.associateController.associateURL = @"common/getVipAddressList";
    search.resultController = VipAddrSearchResultController.new;
    NSMNavigationController *nav = [NSMNavigationController controller:search];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)searchNormal3 {
    NSMSearchController *search = NSMSearchController.new;
    search.associateController.associateURL = @"common/getVipAddressList";
    search.resultController = VipAddrSearchResultController.new;
    NSMNavigationController *nav = [NSMNavigationController controller:search];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)searchSpecial {
    // 点击了之后,不跳转到搜索结果,直接将选中数据返回
    VipAddrSearchHomeController *searchHome = VipAddrSearchHomeController.new;
    searchHome.onClickOther = ^{
        [self onClickNormalAddr:nil];
    };
    searchHome.didSelectSearchItem = ^(id obj) {
        [self onSelectVipAddr:obj completion:nil];
    };
    
    VipAddrSearchAssociateController *searchAssociate = VipAddrSearchAssociateController.new;
    searchAssociate.associateURL = @"common/getVipAddressList";
    searchAssociate.didSelectAssociateItem = ^(id obj) {
        [self onSelectVipAddr:obj completion:nil];
    };
    
    VipAddrSearchResultController *searchResult = VipAddrSearchResultController.new;
    searchResult.didSelectSearchResult = ^(id  _Nonnull obj) {
        [self onSelectVipAddr:obj completion:nil];
    };
    
    NSMSearchController *vip = NSMSearchController.new;
    vip.isPushed = YES;
    vip.homeController = searchHome;
    vip.associateController = searchAssociate;
    vip.resultController = searchResult;
    [self.navigationController pushViewController:vip animated:YES];
}

- (void)onClickNormalAddr:(void (^)(NSString *))completion {
    [self.navigationController pushViewController:UIViewController.new animated:YES];
}

- (void)onSelectVipAddr:(id)obj completion:(void (^)(NSString *))completion {
    // vip地址
    // self.layout.mutableDic[@"vip_addressid"] = SafeString(obj[@"id"]);
    !completion?:completion(SafeString(obj[@"title"]));
    // 此时UI停留在vip地址列表需要返回
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)testImages:(id)obj {
    UIViewController *vc = NSClassFromString(@"ImagesController").new;
    vc.title = SafeString(obj[@"title"]);
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)testImages1:(id)obj {
    UIViewController *vc = NSClassFromString(@"ImagesController1").new;
    vc.title = SafeString(obj[@"title"]);
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)testWithdraw:(id)obj {
    WithdrawController *vc = WithdrawController.new;
    vc.title = SafeString(obj[@"title"]);
    @nsm_weak(self)
    vc.didSubmitSuccess = ^{
        @nsm_strong(self)
        NSLog(@"提现成功");
    };
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)testDelegate:(id)obj {
    UIViewController *vc = NSClassFromString(@"DelegatesController").new;
    vc.title = SafeString(obj[@"title"]);
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)testWebSocket:(id)obj {
    UIViewController *vc = NSClassFromString(@"TestSocketController").new;
    vc.title = SafeString(obj[@"title"]);
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)testSocket:(id)obj {
    UIViewController *vc = NSClassFromString(@"ZRLController").new;
    vc.title = SafeString(obj[@"title"]);
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)testTextField:(id)obj {
    UIViewController *vc = NSClassFromString(@"TextFieldController").new;
    vc.title = SafeString(obj[@"title"]);
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)testProgress:(id)obj {
    UIViewController *vc = NSClassFromString(@"ProgressController").new;
    vc.title = SafeString(obj[@"title"]);
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)imagePick:(id)obj {
    NSMAssetController *vc = NSMAssetController.new;
    vc.title = SafeString(obj[@"title"]);
    vc.sourceType = NSMAssetSourceTypeAll;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)getPhoneDuration:(id)obj {
    
}

- (void)pageScroll:(id)obj {
    UIViewController *vc = NSClassFromString(@"PageController").new;
    vc.title = SafeString(obj[@"title"]);
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)onClickPopItem:(UIBarButtonItem *)sender {
    [self test];
    
    return;
    UIViewController *vc = UIViewController.new;
    vc.view.backgroundColor = UIColor.blueColor;
    vc.preferredContentSize = CGSizeMake(100, 200);
    vc.modalPresentationStyle = UIModalPresentationPopover;
    
    UIPopoverPresentationController *pv = vc.popoverPresentationController;
    pv.backgroundColor = UIColor.redColor;
    pv.barButtonItem = sender;
    pv.permittedArrowDirections = UIPopoverArrowDirectionUp;
    pv.delegate = self;
    [self presentViewController:vc animated:YES completion:nil];
}

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone;
}

- (void)test {
    NSDictionary *param = @{@"city_id": @"027",
                            @"longitude": @"114.31",
                            @"latitude": @"30.52",
                            @"type": @"1",
                            @"page": @"1",
                            @"sign": @"3caebf5e2ecf7c60000f36e29d134585",
                            @"timestamp": @"1572936749"};
    [_networker POST:@"http://huangxy.test.hbbeisheng.com/api/dynamic/get_list.html" parameters:param success:^(NSMResponseObject *responseObject) {
        
    } empty:^(NSMResponseObject *responseObject) {
        
    } failure:^(NSError *error) {
        
    }];
    NSString *url = [@"https://lb3.gg626.com/playGame?gameid=102" stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet];
    NSString *gameUrl = @"https://lb3.gg626.com/playGame?gameid=102";
    NSString *rootUrl = @"https://lb3.gg626.com/mobile/index";
    BOOL open = NO;
    if (open) {
        OpenURL(url);
    } else
        [self pushHTMLController:nil URLString:url animated:YES];
    
    return;
    NSMViewController *vc = NSMViewController.new;
    self.definesPresentationContext = YES;
    
    NSMNavigationController *nav = [NSMNavigationController controller:vc];
    nav.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    NSLog(@"%s", __func__);
}

- (void)presentationControllerWillDismiss:(UIPresentationController *)presentationController {
    NSLog(@"%s", __func__);
}

@end
